using System.Collections.Generic;
using UnityEngine;

public class SmootherForward
{
    private List<Vector3> _history;
    private int _nextUpdateslot;
    private Vector3 _forward;

    public Vector3 smootherForward
    {
        get { return _forward; }
    }

    public SmootherForward(int sampleSize, Vector3 forward)
    {
        _forward = forward;
        _history = new List<Vector3>(sampleSize);
        for (int i = 0; i < sampleSize; i++)
        {
            _history.Add(Vector3.zero);
        }
        _nextUpdateslot = 0;
    }

    public void Update(Vector3 mostRecentValue)
    {
        _history[_nextUpdateslot++] = mostRecentValue;
        if (_nextUpdateslot >= _history.Count)
        {
            _nextUpdateslot = 0;
        }

        Vector3 sum = Vector3.zero;
        foreach (Vector3 value in _history)
        {
            sum += value;
        }

        _forward = sum / _history.Count;
    }
}